package gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.ProductType;
import model.Stack;
import service.LocationNotFoundException;
import service.Service;

public class AddStack extends JFrame {
    private final Controller controller = new Controller();
    private JTextField txtTrays;
    private JComboBox cbTypes;
    private JButton btnAdd, btnCancel;
    private MainFrame owner;

    public AddStack(MainFrame owner) {
	this.owner = owner;
	setSize(300, 120);
	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	setLocation(100, 100);
	setResizable(false);
	setVisible(true);
	setTitle("Add stack");
	getContentPane().setLayout(new GridLayout(3, 0, 0, 0));

	JPanel panel = new JPanel();
	FlowLayout flowLayout = (FlowLayout) panel.getLayout();
	flowLayout.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel);

	JLabel lblTrays = new JLabel("Trays:");
	panel.add(lblTrays);

	txtTrays = new JTextField();
	panel.add(txtTrays);
	txtTrays.setColumns(10);

	JPanel panel_1 = new JPanel();
	FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
	flowLayout_1.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel_1);

	JLabel lblProductType = new JLabel("Product type:");
	panel_1.add(lblProductType);

	Object[] productTypes = Service.getInstance().getProductTypes()
		.toArray();
	cbTypes = new JComboBox(productTypes);
	panel_1.add(cbTypes);

	JPanel panel_2 = new JPanel();
	FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
	flowLayout_2.setAlignment(FlowLayout.RIGHT);
	getContentPane().add(panel_2);

	btnAdd = new JButton("Add");
	panel_2.add(btnAdd);
	btnAdd.addActionListener(controller);

	btnCancel = new JButton("Cancel");
	panel_2.add(btnCancel);
	btnCancel.addActionListener(controller);

    }

    private class Controller implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
	    if (e.getSource() == btnCancel) {
		AddStack.this.dispose();
	    } else if (e.getSource() == btnAdd) {
		ProductType ptype = (ProductType) cbTypes.getSelectedItem();

		try {
		    int trays = Integer.parseInt(txtTrays.getText());

		    try {
			Stack stack = Service.getInstance().addStack(ptype,
				trays);

			if (stack != null) {
			    AddStack.this.owner.getStockPanel().updateView();
			    AddStack.this.dispose();
			}

			else {
			    JOptionPane.showMessageDialog(AddStack.this,
				    "Could not create stack", "Error",
				    JOptionPane.ERROR_MESSAGE);
			}

		    } catch (LocationNotFoundException e1) {
			JOptionPane.showMessageDialog(AddStack.this,
				"Could not find location", "Error",
				JOptionPane.ERROR_MESSAGE);
		    }
		} catch (NumberFormatException ex) {
		    JOptionPane.showMessageDialog(AddStack.this,
			    "Trays should be number", "Error",
			    JOptionPane.ERROR_MESSAGE);
		}

		// System.out.println(txtTrays.getText() + " " +
		// cbTypes.getSelectedItem());
	    }

	}

    }
}
